/*
 *******************************************************************************
 *
 * Copyright (C) 2019-2020 Dialog Semiconductor.
 * This computer program includes Confidential, Proprietary Information
 * of Dialog Semiconductor. All Rights Reserved.
 *
 *******************************************************************************
 */

package com.example.suota.suota.dialogs;

import android.text.TextUtils;
import android.util.Log;

/**
 * Builder used to create an instance of SupportCustomDialogFragment.
 * SupportCustomDialogFragment creates either an AlertDialog using the given setting,
 * or a custom dialog using the given layoutResourceId.
 */
public class DialogFragmentBuilder {

    private static final String TAG = "DialogFragmentBuilder";
    public static final int REBOOT_DIALOG = 502;
    public static final int ERROR = 504;

    private int type = -1;
    private String title;
    private String message;
    private String positive;
    private String negative;
    private int iconResourceId;
    private int layoutResourceId = -1;
    private boolean cancelable = true;
    private boolean cancelableOnTouchOutside = true;

    public int getType() {
        return type;
    }

    public String getTitle() {
        return title;
    }

    public String getMessage() {
        return message;
    }

    public String getPositive() {
        return positive;
    }

    public String getNegative() {
        return negative;
    }

    public int getIconResourceId() {
        return iconResourceId;
    }

    public int getLayoutResourceId() {
        return layoutResourceId;
    }

    public boolean isCancelable() {
        return cancelable;
    }

    public boolean isCancelableOnTouchOutside() {
        return cancelableOnTouchOutside;
    }

    /**
     * Required to be set.
     * Identifier to distinguish the type of the SupportCustomDialogFragment.
     *
     * @param type an identifier
     * @return the builder itself
     */
    public DialogFragmentBuilder setType(int type) {
        this.type = type;
        return this;
    }

    /**
     * Required to be set if AlertDialog mode is going to be used
     * (meaning when layoutResourceId hasn't been set).
     *
     * @param title the title of the alert dialog
     * @return the builder itself
     */
    public DialogFragmentBuilder setTitle(String title) {
        this.title = title;
        return this;
    }

    /**
     * Required to be set if AlertDialog mode is going to be used
     * (meaning when layoutResourceId hasn't been set).
     *
     * @param message the message of the alert dialog
     * @return the builder itself
     */
    public DialogFragmentBuilder setMessage(String message) {
        this.message = message;
        return this;
    }

    /**
     * The positive button text value.
     * If not set, a positive button with "YES" string
     * will be added to the alert dialog.
     * Used only if the AlertDialog mode is going to be used
     * (meaning when layoutResourceId hasn't been set).
     *
     * @param positive the string used for the positive button
     * @return the builder itself
     */
    public DialogFragmentBuilder setPositive(String positive) {
        this.positive = positive;
        return this;
    }

    /**
     * The negative button text value.
     * If not set, negative button will not be added to the alert dialog.
     * Used only if the AlertDialog mode is going to be used
     * (meaning when layoutResourceId hasn't been set).
     *
     * @param negative the string used for the negative button
     * @return the builder itself
     */
    public DialogFragmentBuilder setNegative(String negative) {
        this.negative = negative;
        return this;
    }

    /**
     * The icon of the alert dialog.
     * Used only if the AlertDialog mode is going to be used
     * (meaning when layoutResourceId hasn't been set).
     *
     * @param iconResourceId the id of the drawable icon
     * @return the builder itself
     */
    public DialogFragmentBuilder setIconResourceId(int iconResourceId) {
        this.iconResourceId = iconResourceId;
        return this;
    }

    /**
     * Required to be set if custom dialog mode is going to be used.
     *
     * @param layoutResourceId the layout used to inflate the dialog
     * @return the builder itself
     */
    public DialogFragmentBuilder setLayoutResourceId(int layoutResourceId) {
        this.layoutResourceId = layoutResourceId;
        return this;
    }

    /**
     * Setting the cancelable mode of the dialog.
     *
     * @param cancelable flag
     * @return the builder itself
     */
    public DialogFragmentBuilder setCancelable(boolean cancelable) {
        this.cancelable = cancelable;
        return this;
    }

    /**
     * Setting the cancelable on touch outside mode of the dialog.
     *
     * @param cancelableOnTouchOutside flag
     * @return the builder itself
     */
    public DialogFragmentBuilder setCancelableOnTouchOutside(boolean cancelableOnTouchOutside) {
        this.cancelableOnTouchOutside = cancelableOnTouchOutside;
        return this;
    }

    private boolean notValidBuild() {
        String caller = Thread.currentThread().getStackTrace()[2].getClassName();
        caller = caller.substring(0, caller.lastIndexOf("."));
        caller = caller.substring(0, caller.lastIndexOf("."));

        if (!caller.equals("com.dialog.suotalib")) {
            if (type == -1) {
                Log.e(TAG, "Cannot create SupportCustomDialogFragment without specifying a type");
                return true;
            }
            if ((TextUtils.isEmpty(title) || TextUtils.isEmpty(message)) && layoutResourceId == -1) {
                Log.e(TAG, "Cannot create SupportCustomDialogFragment without either providing title and message for the Alert dialog or providing custom resource layout id");
                return true;
            }
        }
        return false;
    }

    public CustomDialogFragment build() {
        if (notValidBuild())
            return null;

        CustomDialogFragment fragment;
        if (layoutResourceId == -1) {
            fragment = CustomDialogFragment.newInstance(type, title, message, positive, negative, iconResourceId, cancelable, cancelableOnTouchOutside, new DialogFragmentManager(getType(), cancelableOnTouchOutside));
        } else {
            fragment = CustomDialogFragment.newInstance(type, layoutResourceId, cancelable, cancelableOnTouchOutside, new DialogFragmentManager(getType(), cancelableOnTouchOutside));
        }
        return fragment;
    }

    public SupportCustomDialogFragment buildV4() {
        if (notValidBuild())
            return null;

        SupportCustomDialogFragment fragment;
        if (getLayoutResourceId() == -1) {
            fragment = SupportCustomDialogFragment.newInstance(getType(), getTitle(), getMessage(), getPositive(), getNegative(), getIconResourceId(), isCancelable(), isCancelableOnTouchOutside(), new DialogFragmentManager(getType(), cancelableOnTouchOutside));
        } else {
            fragment = SupportCustomDialogFragment.newInstance(getType(), getLayoutResourceId(), isCancelable(), isCancelableOnTouchOutside(), new DialogFragmentManager(getType(), cancelableOnTouchOutside));
        }
        return fragment;
    }
}
